$(function () {
    var tableSource = new $.jqx.dataAdapter({
        dataType: "json",
        dataFields: [{
            name: "id",
            type: "int"
        }, {
            name: "name",
            type: "string"
        }, {
            name: "icon",
            type: "string"
        }],
        id: "id",
        url: "icon/getOneCategory.do"
    });
    $("#dataTable").jqxDataTable({
        width: "100%",
        source: tableSource,
        pageable: true,
        pageSize: 15,
        serverProcessing: true,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [{
            text: "序号",
            width: 50,
            cellsRenderer: function (row) {
                return (row + 1).toString();
            }
        }, {
            text: "名称",
            dataField: "name"
        }, {
            text: "图标样式",
            dataField: "icon",
            cellsRenderer: function (ruw, column, value, rowData) {
                return "<i class='iconfont'>" + value + "</i>";
            }
        }],
    });
    $("#dataTable")
        .on(
            "rowDoubleClick",
            function (info, args) {
                if (args && args.flag) {
                    info = args;
                }
                editingIndex = info.args.index;
                editingData = info.args.row;
                _editerIndex = layer.open({
                    id: "addRecommendGoods",
                    title: "设置一级分类图标",
                    type: 1,
                    skin: "layui-layer-rim",
                    area: ["500px", "500px"],
                    content: '<div id="categoryIcon"></div>',
                });
                getCategoryIcon(editingData);
            });
   });

function getCategoryIcon(rows) {
    var globalUrl = "icon/selectIcon.do?categoryId=" + rows.id;
    var globalSource = {
        datatype: "json",
        datafields: [{name: 'id', type: "int"},
            {name: 'icon_content', type: "string"},
            {name: 'category_id', type: "int"}],
        url: globalUrl,
        async: false
    };

    var globaldataAdapter = new $.jqx.dataAdapter(globalSource);

    $("#categoryIcon")
        .jqxDataTable(
            {
                width: "100%",
                source: globaldataAdapter,
                pageable: true,
                pageSize: 15,
                serverProcessing: true,
                pagerButtonsCount: 10,
                columnsResize: true,
                sortable: true,
                columns: [
                    {
                        text: "序号",
                        width: 50,
                        cellsRenderer: function (row) {
                            return (row + 1).toString();
                        }
                    },
                    {
                        text: "图标样式",
                        dataField: "icon_content",
                        cellsRenderer: function (ruw, column, value, rowData) {
                            return "<i class='iconfont'>" + value + "</i>";
                        }
                    },
                    {
                        text: "操作",
                        cellsRenderer: function (row, column, value, rowData) {
                            // console.log(rowData.icon_content);
                            var c = rowData.icon_content.replace('&', '&amp;');
                            // var array = new Array();
                            // array.push(rows.id);
                            // array.push(c);
                            return "<input type=\"button\" onclick=\"setIcon('"+rows.id+"','"+c+"')\" class=\"btn-sm btn-primary\" value=\"设置\"  />";
                        }
                    }]
            });
}

function setIcon(id, content) {
	content = content.replace('&amp;','&');
    $.ajax({
        url: "icon/updateCategoryIcon.do",
        data: {"id":id,"icon":content},
        async:false,
        success: function (ret) {
        	if(ret.code == 0){
               buildClassMenu();
        	}
        },
        error: function (ret) {
        	alert("操作失败");
        }
    });
}

function buildClassMenu(){
    $.ajax({
        url:"buildClassMenu.do",
        async:false,
        success: function(ret){
            layer.msg('设置一级分类图标成功',function(){
                location.reload(true);
            });
        }
    })
}